
$(document).on("change", '#loanContractCode', function() {
	$.ajax({
		url : "check",
		type : "post",
		data : {
			loanContractCode : $("#loanContractCode").val()
		},
		success : function(res) {
			console.log(res)
			if (res.status == 200) {

			} else {
				toastr.error('该合同案件已存在')
			}
		},
		error : function(e) {
			alert("错误！！");
			window.clearInterval(timer);
		}
	});
})

function case_edit(){
	calculation()
	// 合同信息
	var loanContractCode = $("#loanContractCode").val();
	var mortgageContractCode = $("#mortgageContractCode").val();
	var guaranteeContractCode = $("#guaranteeContractCode").val();
	var consortiumContractCode = $("#consortiumContractCode").val();
	var mortgageContractCode_num = $("#mortgageContractCode_num").val()
	var contractRegisterProvince = $("#cmbProvince").val();
	var contractRegisterCity = $("#cmbCity").val();
	var contractRegisterArea = $("#cmbArea").val();
	var contractRegisterAddress = $("#contractRegisterAddress").val();
	var bankName = $("#bankName").val();
	+$("#bankNameDetails").val();
	var executionStatus = $("#myselect").val();
	var loanAmount = $("#liabilities").val();
	if(!loanAmount){
		loanAmount=null;
	}else{
		loanAmount = parseFloat(loanAmount.replace(/\,/g,""))
	}
	var unpaidPrincipal = $("#principal").val();
	if(!unpaidPrincipal){
		unpaidPrincipal=null;
	}else{
		unpaidPrincipal = parseFloat(unpaidPrincipal.replace(/\,/g,""));
	}
	var interestRate = $("#ratea").val();
	var startDate = $("#data-start").val();
	var repaymentDate = $("#datae-end").val();
	var inter = $("#inter").val();
	if(!inter){
		inter=null;
	}else{
		inter = parseFloat(inter.replace(/\,/g,""))
	}
	var loanTerm = $("#datea").val();
	var timeoutRatesRise = $("#overdataup").val();
    var overDataStart= $("#overData-start").val();
	var overdueRateDate = $("#data-end").val();
	var timeoutRise = $("#overdate").val();
	var over_interest = $("#over_interest").val();
	if(!over_interest){
		over_interest=null;
	}else{
		over_interest = parseFloat(over_interest.replace(/\,/g,""));
	}
	var compoundInterestRate = $("#compoundInterestRate").val();
	if(!compoundInterestRate){
		compoundInterestRate=null;
	}else{
		compoundInterestRate = parseFloat(compoundInterestRate.replace(/\,/g,""));
	}
	var interest = $("#interest").val();
    if(!interest){
    	interest=null;
	}else{
		interest = parseFloat(interest.replace(/\,/g,""))
	}
    var amountPayable_out = $("#sum_out").val();
    if(!amountPayable_out){
    	amountPayable_out=null;
	}else{
		amountPayable_out = parseFloat(amountPayable_out.replace(/\,/g,""))
	}
	var amountPayable = $("#sum").val();
    if(!amountPayable){
    	amountPayable=null;
	}else{
		amountPayable = parseFloat(amountPayable.replace(/\,/g,""))
	}
	var id = $("#case_id").attr("data-caseId");   
		
		

	if (loanContractCode == null || loanContractCode == "") {
		toastr.error('贷款合同不能为空')
		return;
	}
	if (executionStatus == -1) {
		toastr.error('请选择执行状态')
		return;
	}
	if (loanAmount == null || loanAmount == "") {
		toastr.error('贷款金额不能为空')
		return;
	}
	if (unpaidPrincipal == null || unpaidPrincipal == "") {
		toastr.error('未还本金不能为空')
		return;
	}
		

		
		$.ajax({
			url : "edit",
			type : "post",
			data : {
				id : id,
				loanContractCode : loanContractCode,
				mortgageContractCode : mortgageContractCode,
				guaranteeContractCode : guaranteeContractCode,
				consortiumContractCode : consortiumContractCode,
				mortgageContractCode_num : mortgageContractCode_num,
				// contractRegisterProvince: contractRegisterProvince,
				// contractRegisterCity : contractRegisterCity ,
				// contractRegisterArea : contractRegisterArea ,
				contractRegisterAddress : contractRegisterAddress ,
				bankName : bankName,
				executionStatus : executionStatus,
				loanAmount : loanAmount,
				unpaidPrincipal : unpaidPrincipal,
				interestRate : interestRate,
				startDate : startDate,
				repaymentDate : repaymentDate,
				inter : inter,
				loanTerm : loanTerm,
				timeoutRatesRise : timeoutRatesRise,
				overDataStart:overDataStart,
				overdueRateDate : overdueRateDate,
				timeoutRise : timeoutRise,
				over_interest : over_interest,
				compoundInterestRate : compoundInterestRate,
				interest : interest,
				amountPayable_out : amountPayable_out,
				amountPayable : amountPayable,
			},
			success : function(res) {
				
				if(res.status == 200){
					toastr.success("案件修改成功")
				} else {
					toastr.error('案件修改失败')
				}
			},
			error : function(e) {
				alert("错误！！");
				window.clearInterval(timer);
			}
		});
		
	}
function checkcese() {
	showModal({
		className : "",
		width : "223px",
		title : "提示",
		content : "是否确定修改",
		showCancel : true,
		cancelText : '否',
		cancelColor : "#ffffff",
		confirmText : '是',
		confirmColor : '#ffffff',
		success : function(res) {
			if (res.confirm) {
				case_edit();
			}
		}
	});
}

function deldebtor(id) {
	showModal({
		className : "",
		width : "223px",
		title : "提示",
		content : "是否确定删除",
		showCancel : true,
		cancelText : '否',
		cancelColor : "#ffffff",
		confirmText : '是',
		confirmColor : '#ffffff',
		success : function(res) {
			if (res.confirm) {
				del(id);
			}
		}
	});

}
function del(id) {
	console.log("id="+id)
	console.log("case_id="+$("#case_id").attr("data-caseId"))
	$.ajax({
		url : "../debtor/delById",
		type : "post",
		data : {
			id : id,
			caseId : $("#case_id").attr("data-caseId")
		},
		success : function(res) {

			if (res.status == 200) {
				toastr.success("人员已经删除")
			} else {
				toastr.error('人员删除失败')
			}
		},
		error : function(e) {
			alert("错误！！");
			window.clearInterval(timer);
		}
	});
}

function checkJudgment() {
	showModal({
		className : "",
		width : "223px",
		title : "提示",
		content : "是否确定修改",
		showCancel : true,
		cancelText : '否',
		cancelColor : "#ffffff",
		confirmText : '是',
		confirmColor : '#ffffff',
		success : function(res) {
			if (res.confirm) {
				casejudgment_edit();
			}
		}
	});
}

function casejudgment_edit(){
	// 判决信息
	var verdict_code = $("#verdict_code").val();
	var verdict_date = $("#verdict_date").val();
	var agreed_execution_time = $("#agreed_execution_time").val();
	var entry_into_force_time = $("#entry_into_force_time").val();
	var judge = $("#judge").val();
	var judge_phone = $("#judge_phone").val();
	var clerk = $("#clerk").val();
	var clerk_phone = $("#clerk_phone").val();
	var defendant = $("#defendant").val();
	var lawyer = $("#lawyers").val();
	var verdict = $("#sentence_liabilities").val();
	if(!verdict){
		verdict=null;
	}else{
		verdict = parseFloat(verdict.replace(/\,/g,""));
	}
	var Judgement_interest = $("#sentence_rate").val();
	if(!Judgement_interest){
		Judgement_interest=null;
	}else{
		Judgement_interest = parseFloat(Judgement_interest.replace(/\,/g,""));
	}
	var fine_interest = $("#sentence_fine").val();
	if(!fine_interest){
		fine_interest=null;
	}else{
		fine_interest = parseFloat(fine_interest.replace(/\,/g,""));
	}
	var lawyer_cost = $("#lawyer").val();
	if(!lawyer_cost){
		lawyer_cost=null;
	}else{
		lawyer_cost = parseFloat(lawyer_cost.replace(/\,/g,""))
	}
	var litigation_cost = $("#costs").val();
	if(!litigation_cost){
		litigation_cost=null;
	}else{
		litigation_cost = parseFloat(litigation_cost.replace(/\,/g,""));
	}
	var preservation_cost = $("#insurance").val();
	if(!preservation_cost){
		preservation_cost=null;
	}else{
		preservation_cost = parseFloat(preservation_cost.replace(/\,/g,""));
	}
	var publicity_cost = $("#publicity").val();
	if(!publicity_cost){
		publicity_cost=null;
	}else{
		publicity_cost = parseFloat(publicity_cost.replace(/\,/g,""))
	}
	var implement_judge = $("#implement_judge").val();
	var implement_phone = $("#implement_phone").val();
	var implement_assistant = $("#implement_assistant").val();
	var assistant_phone = $("#assistant_phone").val();
	var implement_no = $("#implement_no").val();
	var implement_sum = $("#implement_sum").val();
	if(!implement_sum){
		implement_sum=null;
	}else{
		implement_sum = parseFloat(implement_sum.replace(/\,/g,""));
	}
	var overdue_fine_start_date = $("#delay_start").val();
	var overdue_fine_closing_date = $("#delay_end").val();
	var late_fee_Proportion = $("#late_fee_Proportion").val();
	var delay_days = $("#delay_date").val();
	var overdue_fine = $("#delay_money").val();
	if(!overdue_fine){
		overdue_fine=null;
	}else{
		overdue_fine = parseFloat(overdue_fine.replace(/\,/g,""));
	}
	var overdata_start= $("#overdata-start").val();
	var overdata_end= $("#overdata-end").val();
	var overdate_interest= $("#overdate-interest").val();
	var compound_interest = $("#compound_interest").val();
	var preservation = $("#preservation").val();
	var overdue_interes = $("#overdue_interes").val();
	if(!overdue_interes){
		overdue_interes=null;
	}else{
		overdue_interes = parseFloat(overdue_interes.replace(/\,/g,""));
	}
	var interest_sum = $("#interest_sum").val();
	if(!interest_sum){
		interest_sum=null;
	}else{
		interest_sum = parseFloat(interest_sum.replace(/\,/g,""));
	}
	var sentence_sum_notcompound =$("#sentence_sum_notcompound").val();
	if(!sentence_sum_notcompound){
		sentence_sum_notcompound=null;
	}else{
		sentence_sum_notcompound = parseFloat(sentence_sum_notcompound.replace(/\,/g,""));
	}
	var total_cost = $("#sentence_sum").val();
	if(!total_cost){
		total_cost=null;
	}else{
		total_cost = parseFloat(total_cost.replace(/\,/g,""));
	}
	var case_id = $("#case_id").attr("data-caseId");                      
		if(case_id == null || case_id == ""){
			toastr.error('未获取到案件请重新加载页面')
			return;
		}
		if(Judgement_interest == null || Judgement_interest == ""){
			toastr.error('判决/调解利息不可为空')
			return;
		}
		if(verdict == null || verdict == ""){
			toastr.error('判决/调解本金不可为空')
			return;
		}
		
		if(defendant == null || defendant == ""){
			toastr.error('被告人不能为空')
			return;
		}
		if(judge == null || judge == ""){
			toastr.error('主法官不能为空')
			return;
		}
		if(verdict_date == null || verdict_date == ""){
			toastr.error('判决/调解日期')
			return;
		}
		if(verdict_code == null || verdict_code == ""){
			toastr.error('判决/调解/受理案号	')
			return;
		}
		

		
		$.ajax({
			url : "../judgment/edit",
			type : "post",
			data : {                      
				verdict_code : verdict_code,
				verdict_date : verdict_date,
				agreed_execution_time : agreed_execution_time,
				entry_into_force_time : entry_into_force_time,
				judge : judge,
				judge_phone : judge_phone,
				clerk : clerk,
				clerk_phone : clerk_phone,
				defendant : defendant,
				lawyer : lawyer,
				verdict : verdict,
				Judgement_interest : Judgement_interest,
				fine_interest : fine_interest,
				lawyer_cost : lawyer_cost,
				litigation_cost : litigation_cost,
				preservation_cost : preservation_cost,
				publicity_cost : publicity_cost,
				implement_judge : implement_judge,
				implement_phone : implement_phone,
				implement_assistant : implement_assistant,
				assistant_phone : assistant_phone,
				implement_no : implement_no,
				implement_sum : implement_sum,
				overdue_fine_start_date : overdue_fine_start_date,
				overdue_fine_closing_date : overdue_fine_closing_date,
				late_fee_Proportion : late_fee_Proportion,
				delay_days : delay_days,
				overdue_fine : overdue_fine,
				overdata_start : overdata_start,
				overdata_end : overdata_end,
				overdate_interest : overdate_interest,
				compound_interest : compound_interest,
				preservation : preservation,
				overdue_interes : overdue_interes,
				interest_sum : interest_sum,
				sentence_sum_notcompound : sentence_sum_notcompound,
				total_cost : total_cost,
				case_id : case_id             
			},
			success : function(res) {
				
				if(res.status == 200){
					toastr.success("案件已修改")
				} else {
					toastr.error('案件修改失败')
				}
			},
			error : function(e) {
				alert("错误！！");
				window.clearInterval(timer);
			}
		});
		
	}
function to_adddebtox(){
	$.ajax({
		url : "../debtor/queryDebtor",
		type : "post",
		data : {
			debtor_name : $("#namex").val(),
			debtor_card_id : $("#idcardx").val(),
		},
		success : function(res) {
			
			if (res.status == 200) {
				if(!(!$("#idcardx").val())){
					add_debtox(res.data)
					return;
				}
				$("#debtorId").attr("data-id",res.data);
				$("#aaaa").click();
			} else {
				add_debtox();
			}
		},
		error : function(e) {
			alert("错误！！");
			window.clearInterval(timer);
		}
	});
}
function to_adddebtorx(){
	$.ajax({
		url : "../debtor/queryDebtor",
		type : "post",
		data : {
			debtor_name : $("#name").val(),
			debtor_card_id : $("#idcard").val(),
		},
		success : function(res) {

			if (res.status == 200) {
				if(!(!$("#idcard").val())){
					add_debto(res.data)
					return;
				}
				$("#debtorIdz").attr("data-id",res.data);
				$("#bbbb").click();
			} else {
				add_debto();
			}
		},
		error : function(e) {
			alert("错误！！");
			window.clearInterval(timer);
		}
	});
}


function add_olddebtox(){
	add_debtox($("#debtorId").attr("data-id"))
}
function add_olddebto(){
	add_debto($("#debtorIdz").attr("data-id"))
}

	function add_debtox(id) {
		if(!id){
			id=null;
		}
		var identity1 = $("#identity1").val();
		if ($("#case_id").attr("data-caseId") == null
				|| $("#case_id").attr("data-caseId") == "") {
			toastr.error('获取案件失败，请刷新页面')
			return;
		}
		if ($("#namex").val() == null || $("#namex").val() == "") {
			alert("姓名不可为空");
			return;
		}
		$.ajax({
			url : "../debtor/add",
			type : "post",
			data : {
				name : $("#namex").val(),
				idcard : $("#idcardx").val(),
				type : $("#identityx").val(),
				caseId : $("#case_id").attr("data-caseId"),
				identity : identity1,
				id : id
			},
			success : function(res) {
				if (res.status == 200) {
					toastr.success("添加成功")
					if (!$("#idcardx").val()) {
			debtor += 1;
			var deba = "deba" + Number(debtor);
			var debb = "debb" + Number(debtor);
			var debc = "debc" + Number(debtor);
			$("#debtorx")
					.append(
							"<div class='col-lg-4'>"
									+ "<div class='contact-box'>"
									+ "<button type='button' class='close' style='margin-bottom: -7px'>×</button>"
									+ "<div class='col-sm-4'>"
									+ "<div class='text-center'>"
									+ "<div class='m-t-xs font-bold'   id="
									+ deba
									+ ">"
									+ $("#identityx").val()
									+ "</div>"
									+ "</div>"
									+ "</div>"
									+ "<div class='col-sm-8'>"
									+ "<h3><strong   id="
									+ debb
									+ ">"
									+ $("#namex").val()
									+ "</strong></h3>"
									+ "<p>&nbsp;</p>"
									+ "</div>"
									+ "<div class='clearfix'></div>"
									+ "</div>" + "</div>")
		}else {
				debtor += 1;
				var deba = "deba" + Number(debtor);
				var debb = "debb" + Number(debtor);
				var debc = "debc" + Number(debtor);
				$("#debtorx")
						.append(
								"<div class='col-lg-4'>"
										+ "<div class='contact-box'>"
										+ "<button type='button' class='close' style='margin-bottom: -7px'>×</button>"
										+ "<div class='col-sm-4'>"
										+ "<div class='text-center'>"
										+ "<div class='m-t-xs font-bold'   id="
										+ deba
										+ ">"
										+ $("#identityx").val()
										+ "</div>"
										+ "</div>"
										+ "</div>"
										+ "<div class='col-sm-8'>"
										+ "<h3><strong   id="
										+ debb
										+ ">"
										+ $("#namex").val()
										+ "</strong></h3>"
										+ "<p   id="
										+ debc
										+ ">"
										+ $("#idcardx").val()
										+ "</p>"
										+ "</div>"
										+ "<div class='clearfix'></div>"
										+ "</div>" + "</div>")
			}
				} else {
					toastr.error('添加失败')
				}
			},
			error : function(e) {
				alert("错误！！");
				window.clearInterval(timer);
			}
		});

	}

	function add_debto(id) {
		
		if(!id){
			id=null;
		}
		var identity = $("#identity").val();
		if ($("#case_id").attr("data-caseId") == null
				|| $("#case_id").attr("data-caseId") == "") {
			toastr.error('请先添加案件')
			return;
		}
		if ($("#name").val() == null || $("#name").val() == "") {
			alert("姓名不可为空");
			return;
		}

		$.ajax({
			url : "../debtor/add",
			type : "post",
			data : {
				name : $("#name").val(),
				idcard : $("#idcard").val(),
				type : "债务人",
				caseId : $("#case_id").attr("data-caseId"),
				identity : identity,
				id : id
			},
			success : function(res) {

				if (res.status == 200) {
					toastr.success("添加成功")
					if (!$("#idcard").val()) {
			debtor += 1;
			var deba = "deba" + Number(debtor);
			var debb = "debb" + Number(debtor);
			var debc = "debc" + Number(debtor);
			$("#debtor")
					.append(
							"<div class='col-lg-4'>"
									+ "<div class='contact-box'>"
									+ "<button type='button' class='close' style='margin-bottom: -7px'>×</button>"
									+ "<div class='col-sm-4'>"
									+ "<div class='text-center'>"
									+ "<div class='m-t-xs font-bold' id ="
									+ deba
									+ ">债务人</div>"
									+ "</div>"
									+ "</div>"
									+ "<div class='col-sm-8'>"
									+ "<h3><strong name='debtor_name' id="
									+ debb
									+ ">"
									+ $("#name").val()
									+ "</strong></h3>"
									+ "<p>&nbsp;</p>"
									+ "</div>"
									+ "<div class='clearfix'></div>"
									+ "</div>" + "</div>")
		} else {
				debtor += 1;
				var deba = "deba" + Number(debtor);
				var debb = "debb" + Number(debtor);
				var debc = "debc" + Number(debtor);
				$("#debtor")
						.append(
								"<div class='col-lg-4'>"
										+ "<div class='contact-box'>"
										+ "<button type='button' class='close' style='margin-bottom: -7px'>×</button>"
										+ "<div class='col-sm-4'>"
										+ "<div class='text-center'>"
										+ "<div class='m-t-xs font-bold' id="
										+ deba
										+ ">债务人</div>"
										+ "</div>"
										+ "</div>"
										+ "<div class='col-sm-8'>"
										+ "<h3><strong  id="
										+ debb
										+ ">"
										+ $("#name").val()
										+ "</strong></h3>"
										+ "<p  id="
										+ debc
										+ ">"
										+ $("#idcard").val()
										+ "</p>"
										+ "</div>"
										+ "<div class='clearfix'></div>"
										+ "</div>" + "</div>")
			}
				} else {
					toastr.error('添加失败')
				}
			},
			error : function(e) {
				alert("错误！！");
				window.clearInterval(timer);
			}
		});

	}
